// 获取app实例对象
let app = getApp();
Page({
  data: {
    hisArr: [],   //历史记录数组
    array: [],    //热门搜索数组
    hisShow: false,  //历史记录显隐
    value: '',   //输入框内容
    topCity: '郑州',
  },
  getHisList(){
    let list = my.getStorageSync({ key: 'historyArray' }).data
    if(list){
      this.setData({
        hisArr: list,
        hisShow: true
      })
    }
  },
  onLoad() {
  },
  onShow() {
    this.getHisList()
  },
  selectCity(){
    const that = this
    my.chooseCity({
      showLocatedCity: true,
      showHotCities: true,
      setLocatedCity: true,
      success: (res) => {
        that.setData({
                    topCity: res.city
         })
        // that.getList(false);
      },
    });
    // my.getLocation({
    //   type: 1,
    //   success(res) {
    //     that.setData({
    //       topCity: res.city.slice(0, 2)
    //     })
    //     my.chooseCity({
    //       // showLocatedCity: true,
    //       showHotCities: true,
    //       setLocatedCity: true,
    //       success: (res) => {
    //         that.setData({
    //           topCity: res.city
    //         })
    //       },
    //     });
    //   },
    // });
  },
  // 点击历史记录
  changeValue(e) {
    let text = e.currentTarget.dataset.text;
    this.setData({
      value: text
    })
    my.navigateTo({ url: '../productList/productList?title=' + text })
  },
  handleInput(value) {
    this.setData({
      value,
    });
  },
  handleClear() {
    this.setData({
      value: '',
    });
  },
  jumpProductList(val, arr){
    const that = this
    my.setStorage({
      key: 'historyArray',
      data: arr,
      success: function () {
        my.navigateTo({ url: '../productList/productList?title=' + val })
        that.setData({
          value: ""
        })
      }
    })

  },
  handleSubmit(value) {
    // 搜索内容为空时，不进行下一步
    if (!value) {
      return
    }
    const that = this
    let historySearch = []
    if (!my.getStorageSync({ key: 'historyArray' }).data) {
      // 第一次存
      let historyItem = { item: that.data.value }
      historySearch.push(historyItem);
      this.jumpProductList(value, historySearch)
    } else {
      // 之后存
      historySearch = [...my.getStorageSync({ key: 'historyArray' }).data]
      let historyItem = { item: that.data.value }
      historySearch.unshift(historyItem);
      //去重逻辑
      let arr = [];
      const res = new Map();
      arr = historySearch.filter((a) => !res.has(a.item) && res.set(a.item, 1));
      historySearch = [...arr];
      if (historySearch.length > 6) {
        historySearch.pop()
      }
      this.jumpProductList(value, historySearch)
    }
  },
  // 搜索按钮的点击事件
  serchBtn() {
    const that = this
    let historySearch = []
    if (!my.getStorageSync({ key: 'historyArray' }).data) {
      // 第一次存
      let historyItem = { item: that.data.value }
      historySearch.push(historyItem);
      this.jumpProductList(that.data.value, historySearch)
    } else {
      // 之后存
      historySearch = [...my.getStorageSync({ key: 'historyArray' }).data]
      let historyItem = { item: that.data.value }
      historySearch.unshift(historyItem);
      //去重逻辑
      let arr = [];
      const res = new Map();
      arr = historySearch.filter((a) => !res.has(a.item) && res.set(a.item, 1));
      historySearch = [...arr];
      if (historySearch.length > 6) {
        historySearch.pop()
      }
      this.jumpProductList(that.data.value, historySearch)
    }
  },
  delHistory() {
    const that = this
    my.removeStorage({
      key: 'historyArray',
      success: function () {
        that.setData({
          hisShow: false
        })
      }
    });
  },
  hotSearch(e) {
    let text = e.currentTarget.dataset.item;
  },
});
